<template>
  <div class="page">
    <s-header :name="'投诉举报'" :back="'/user/complaint'"></s-header>
    <van-form ref="dataFormRef" class="complaint" label-width="100px">
      <div class="card-box">
        <div class="card-item">
          <div class="card-header">投诉人信息</div>
          <van-divider />
          <van-field v-model="dataForm.complainantName" :rules="dataRule.complainantName" required label="姓名" placeholder="请输入姓名" />
          <van-field
            v-model="dataForm.complainantIdcard"
            :rules="[{ validator: checkIdCard }]"
            required
            label="身份证号"
            maxlength="18"
            placeholder="请输入身份证号"
          />
          <van-field
            v-model="dataForm.complainantPhone"
            :rules="[{ validator: checkphone }]"
            required
            label="联系方式"
            maxlength="11"
            type="tel"
            placeholder="请输入联系方式"
          />
        </div>
      </div>
      <div class="card-box">
        <div class="card-item">
          <div class="card-header">投诉对象</div>
          <van-divider />
          <van-field v-model="dataForm.complaintObject" :rules="dataRule.complaintObject" required label="投诉对象" placeholder="请输入投诉对象" />
          <van-field
            v-model="dataForm.complaintLocation"
            :rules="dataRule.complaintLocation"
            required
            label="投诉对象位置"
            placeholder="请输入投诉对象位置"
          />
          <van-field
            v-model="dataForm.complaintContent"
            :rules="dataRule.complaintContent"
            required
            label="投诉内容"
            type="textarea"
            rows="2"
            autosize
            placeholder="请输入投诉内容"
          />
          <van-field label="相关照片" />
          <div class="card-body">
            <image-upload v-model:value="pictures" fileType="complaint" @uploadBack="uploadBack"></image-upload>
          </div>
        </div>
      </div>
      <div class="btn-group">
        <van-button type="button" class="cus-btn btn-default" @click="handleToRouter('/user/complaint')">取消</van-button>
        <van-button :loading="isCommit" type="button" class="cus-btn btn-warning" @click="handleSubmit">提交举报</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { reactive, toRefs, ref, onMounted } from 'vue'
import sHeader from '@/components/SimpleHeader'
import { useRoute, useRouter } from 'vue-router'
import ImageUpload from '@/components/ImageUpload'
import { saveComplaint } from '@/service/user'
import { Toast } from 'vant'
export default {
  components: {
    sHeader,
    ImageUpload,
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const dataFormRef = ref(null)
    const state = reactive({
      isCommit: false,
      chosenAddressId: '1',
      list: [],
      from: route.query.from,
      pictures: [],
      allFileList: [],
      isDetail: false,
      dataForm: {
        complainantName: '',
        complainantIdcard: '',
        complainantPhone: '',
        complaintObject: '',
        complaintLocation: '',
        complaintContent: '',
        photos: [],
      },
      dataRule: {
        complainantName: [{ required: true, message: '请输入姓名', trigger: 'onBlur' }],
        complainantIdcard: [{ required: true, validator: checkIdCard, trigger: 'onBlur' }],
        complainantPhone: [{ required: true, validator: checkphone, trigger: 'onBlur' }],
        complaintObject: [{ required: true, message: '请输入投诉对象', trigger: 'onBlur' }],
        complaintLocation: [{ required: true, message: '请输入投诉对象位置', trigger: 'onBlur' }],
        complaintContent: [{ required: true, message: '请输入投诉内容', trigger: 'onBlur' }],
      },
    })

    onMounted(async () => {})

    // 文件上传后回调
    const uploadBack = (fileList) => {
      state.pictures = fileList
    }

    const handleToRouter = (r, query) => {
      router.push({ path: r, query: query || {} })
    }

    const handleSubmit = () => {
      dataFormRef.value.validate().then(() => {
        let fileList = state.pictures
        fileList.forEach((item, index) => {
          item.sort = index
          item.url = item.filePath ? item.filePath : item.url
        })

        let params = state.dataForm

        params.photos = fileList
        state.isCommit = true
        saveComplaint(params)
          .then(() => {
            Toast.success('添加投诉成功')
            setTimeout(() => {
              router.push({ path: '/user/complaint' })
            }, 500)
            state.isCommit = false
          })
          .catch(() => {
            state.isCommit = false
          })
      })
    }
    const checkphone = (val) => {
      if (!val) {
        return '请输入手机号'
      } else if (!/^1(3|4|5|6|7|8)\d{9}$/.test(val)) {
        return '请输入正确的手机号'
      }
    }
    const checkIdCard = (val) => {
      if (!val) {
        return '请输入身份证号'
      } else if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(val)) {
        return '请输入正确的身份证号'
      } else {
        return ''
      }
    }
    return {
      dataFormRef,
      ...toRefs(state),
      handleSubmit,
      handleToRouter,
      uploadBack,
      checkphone,
      checkIdCard,
    }
  },
}
</script>

<style lang="less" scoped>
@import '@/common/style/mixin';
.page {
  .complaint {
    margin-top: 15px;
    .van-divider {
      margin: 10px 0 0;
    }
    /deep/ .van-field__label {
      font-size: 15px;
      font-weight: 400;
      color: #9297ae;
    }
    .card-body {
      .cell-box {
        // display: flex;
        &:after,
        &:before {
          display: table;
          content: '';
        }
        &:after {
          clear: both;
        }

        .value {
          font-size: 15px;
          font-weight: 400;
          color: #2e3f6a;
          position: relative;
          margin-left: 110px;
          line-height: 25px;
        }
      }
    }
    .btn-group {
      padding: 20px 0;
      .cus-btn {
        width: 167px;
      }
    }
  }
}
</style>
